package com.showtao.front.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.showtao.common.dto.ResultDto;
import com.showtao.common.dto.ResultDtoFactory;
import com.showtao.common.dto.widget.DataTablesRequestDto;
import com.showtao.common.dto.widget.DataTablesResponseDto;
import com.showtao.common.validation.ValidateException;
import com.showtao.front.domain.VehicleBrand;
import com.showtao.front.domain.VehicleModel;
import com.showtao.front.domain.VehicleModelFeature;
import com.showtao.front.dto.VehicleLineDto;
import com.showtao.front.dto.VehicleModelDto;
import com.showtao.front.dto.upstream.VehicleBrandSavingDto;
import com.showtao.front.dto.upstream.VehicleLineSavingDto;
import com.showtao.front.dto.upstream.VehicleModelSavingDto;
import com.showtao.front.service.VehicleService;

@Controller
public class VehicleController {

    @Autowired
    private transient VehicleService vehicleService;

    @RequestMapping(value = "/vehicles")
    @ResponseBody
    public ResultDto getVehicles() {
        return ResultDtoFactory.toAck(vehicleService.findAllVehicleBrandMap());
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_brands", method = RequestMethod.POST)
    @ResponseBody
    public DataTablesResponseDto<VehicleBrand> doSearchVehicleBrands(
            @Valid @RequestBody DataTablesRequestDto requestDto, 
            BindingResult result) throws Exception {
        //判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }  
        // 组装Pageable
        Pageable pageable = requestDto.toPagable();
        // 调用Service接口获得具体的分页数据Page<VehicleBrand>
        Page<VehicleBrand> page = vehicleService.findVehicleBrands(pageable);
        // 新建DataTablesResponseDto并以分页数据填充该Dto
        DataTablesResponseDto<VehicleBrand> responseDto = 
                new DataTablesResponseDto<VehicleBrand>();
        return responseDto.populateFromPage(page, requestDto.getDraw());
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_brand", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto doSaveVehicleBrand(
            @Valid @RequestBody VehicleBrandSavingDto dto, 
            BindingResult result) throws Exception {
        //判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }  
        vehicleService.saveVehicleBrand(dto);
        return ResultDtoFactory.toAck("车辆品牌创建成功");
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_brand/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultDto doDeleteVehicleBrand(@PathVariable("id") Long id) throws Exception {
        vehicleService.deleteVehicleBrand(id);
        return ResultDtoFactory.toAck("车辆品牌删除成功");
    }
       
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_lines", method = RequestMethod.POST)
    @ResponseBody
    public DataTablesResponseDto<VehicleLineDto> doSearchVehicleLines(
            @Valid @RequestBody DataTablesRequestDto requestDto, 
            BindingResult result) throws Exception {
        //判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }  
        // 组装Pageable
        Pageable pageable = requestDto.toPagable();
        // 调用Service接口获得具体的分页数据Page<VehicleBrand>
        Page<VehicleLineDto> page = vehicleService.findVehicleLines(pageable);
        // 新建DataTablesResponseDto并以分页数据填充该Dto
        DataTablesResponseDto<VehicleLineDto> responseDto = 
                new DataTablesResponseDto<VehicleLineDto>();
        return responseDto.populateFromPage(page, requestDto.getDraw());
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_line", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto doSaveVehicleLine(
            @Valid @RequestBody VehicleLineSavingDto dto, 
            BindingResult result) throws Exception {
        //判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }  
        vehicleService.saveVehicleLine(dto);
        return ResultDtoFactory.toAck("车系创建成功");
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_line/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultDto doDeleteVehicleLine(@PathVariable("id")Long id) throws Exception {
        vehicleService.deleteVehicleLine(id);
        return ResultDtoFactory.toAck("车系删除成功");
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_models", method = RequestMethod.POST)
    @ResponseBody
    public DataTablesResponseDto<VehicleModelDto> doSearchVehicleModels(
            @Valid @RequestBody DataTablesRequestDto requestDto, 
            BindingResult result) throws Exception {
        //判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }  
        // 组装Pageable
        Pageable pageable = requestDto.toPagable();
        // 调用Service接口获得具体的分页数据Page<VehicleBrand>
        Page<VehicleModelDto> page = vehicleService.findVehicleModels(pageable);
        // 新建DataTablesResponseDto并以分页数据填充该Dto
        DataTablesResponseDto<VehicleModelDto> responseDto = 
                new DataTablesResponseDto<VehicleModelDto>();
        return responseDto.populateFromPage(page, requestDto.getDraw());
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_model", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto doSaveVehicleModel(
            @Valid @RequestBody VehicleModelSavingDto dto, 
            BindingResult result) throws Exception {
        //判断是否有验证错误
        if (result.hasErrors()) {
            throw new ValidateException(result);
        }  
        vehicleService.saveVehicleModel(dto);
        return ResultDtoFactory.toAck("车型创建成功");
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_model/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResultDto doDeleteVehicleModel(@PathVariable("id") Long id) throws Exception {
        vehicleService.deleteVehicleModel(id);
        return ResultDtoFactory.toAck("车型删除成功");
    }
    
    @RequiresPermissions (value={"vehicle_admin"})
    @RequestMapping(value = "/admin/vehicle_model/{id}/features")
    @ResponseBody
    public ResultDto getVehicleModelFeatures(@PathVariable("id") Long id) throws Exception {
        Map<String,List<VehicleModelFeature>> map = vehicleService.getModelFeatureMap(id);
        return ResultDtoFactory.toAck(map);
    }
    
    @RequestMapping(value = "/mobile/selection/findModelByKeyWords", method = RequestMethod.POST)
    @ResponseBody
	public ResultDto findModelByKeyWords(Model model, String keywords) throws Exception {
    	List<VehicleModelDto> vehicleModels = new ArrayList<VehicleModelDto>();
    	try{
    		vehicleModels = vehicleService.findLimitedVehicleModelByKeywords(keywords);
    	}catch(Exception ex){
    		return ResultDtoFactory.toAck("errorMsg",ex.getMessage());
    	}
    	if(vehicleModels.size() == 0){
    		return ResultDtoFactory.toAck("errorMsg","没有相关车型");
    	}
		return ResultDtoFactory.toAck(vehicleModels);
	}
}
